-- FUNCTION: public.udf_uiReport_Receipts_Admissions_Location(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)

-- 
DROP FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone);

CREATE OR REPLACE FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(
	"accountId" integer[] DEFAULT NULL::integer[],
	"providerId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer,
	"roleId" integer[] DEFAULT NULL::integer[],
	"admissionNo" text DEFAULT NULL::text,
	"patientId" integer DEFAULT NULL::integer,
	"uMRNo" character varying DEFAULT NULL::text,
	"patientMobile" character varying DEFAULT NULL::text,
	"receiptId" integer DEFAULT NULL::integer,
	"createdBy" integer DEFAULT NULL::integer,
	"payTypeId" integer DEFAULT NULL::integer,
	"fromDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"toDate" timestamp without time zone DEFAULT (now())::timestamp without time zone)
    RETURNS TABLE("ReceiptCreatedBy" text, "RoleName" character varying,
				  "ReceiptDate" timestamp without time zone, "ReceiptId" text, "AdmissionNo" text,
				  "AdmissionDate" date, "AdmissionTime" text, "PatientName" text,
				  "UMRNo" character varying, "PatientMobile" character varying,
				  "ProviderName" character varying, "PayTypeName" character varying,"PaymentDetails" character varying,
				  "PaidAmount" numeric, "RefundAmount" numeric, "BalanceAmount" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN

return query 

select coalesce(A."FullName",'GrandTotal') "ReceiptCreatedBy",A."RoleName",A."CreatedDate"
"ReceiptDate",coalesce(A."ReceiptId"::text,'Total')"ReceiptId"
,A."AdmissionNo",A."AdmissionDate"::date as "AdmissionDate",to_char(A."AdmissionDate" , 'HH12:MI PM' ) "AdmissionTime",
A."PatientName" "PatientName" ,A."UMRNo",A."Mobile" , A."ProviderName" "ProviderName",
A."PayTypeName",A."PaymentDetails",sum(A."PaidAmount") "PaidAmount",sum(A."RefundAmount") "RefundAmount" 

,sum(A."PaidAmount")-sum(A."RefundAmount") "BalanceAmount"
from (

select R."ReceiptId",A."FullName",Rl."RoleName",R."CreatedDate" "CreatedDate",
coalesce(case when "ReceiptTypeId"=1 then R."Cost" end,0) as "PaidAmount" ,
coalesce(case when "ReceiptTypeId"=2 then R."Cost" end,0) as "RefundAmount" ,Ad."AdmissionNo",ad."AdmissionDate",
	ad."AdmissionTime",Ad."LocationId",
pa."FullName" "PatientName",pa."UMRNo",pa."Mobile",pr."FullName" as "ProviderName",
pt."PayTypeName",R."PaymentDetails"
	from "Receipt" R
join "Admission" ad on Ad."AdmissionId"=R."AdmissionId" and ad."Active" is not false
join "Provider" pr on pr."ProviderId"= ad."ProviderId"
join "Patient" pa on Ad."PatientId"=pa."PatientId"
join "Account" A on A."AccountId"=R."CreatedBy"
join "Role" Rl on Rl."RoleId"=A."RoleId"
join "PayType" pt on pt."PayTypeId"=R."PayTypeId"
	where R."Active" is not false and
case when "accountId" is null then 1=1 else  R."CreatedBy"  = any("accountId") end 
and	case when "providerId" is null then 1=1 else  pr."ProviderId"  = "providerId" end 
and case when "locationId" is null then 1=1 else Ad."LocationId" = "locationId" end
	and	case when "roleId" is null then 1=1 else  A."RoleId"  = any("roleId") end  
	and	case when "admissionNo" is null then 1=1 else  Ad."AdmissionNo"  ilike'%'|| "admissionNo"||'%' end
	and	case when "patientId" is null then 1=1 else  pa."PatientId"  = "patientId" end  
	and	case when "uMRNo" is null then 1=1 else  pa."UMRNo"  ilike'%'|| "uMRNo"||'%' end
	and	case when "patientMobile" is null then 1=1 else  pa."Mobile"  ilike'%'|| "patientMobile"||'%' end
		and	case when "receiptId" is null then 1=1 else  R."ReceiptId"  = "receiptId" end  
and 	case when "createdBy" is null then 1=1 else  R."CreatedBy"  = "createdBy" end  
and
case when "payTypeId" is null then 1=1  else R."PayTypeId" ="payTypeId" end  and
case when "fromDate" is null then 1=1 else "fromDate" <=R."CreatedDate" and R."CreatedDate" <="toDate" end 		
) A
GROUP BY GROUPING SETS((A."ReceiptId",A."RoleName",A."FullName",A."CreatedDate",A."PayTypeName",A."PaymentDetails",A."AdmissionNo",
						A."AdmissionDate",A."AdmissionTime",A."PatientName",A."UMRNo",A."Mobile",A."ProviderName" ), (A."FullName",
																									 A."CreatedDate"), ())     
order by A."FullName" 
 ;

END-- FUNCTION: public.udf_uiReport_Receipts_Admissions_Location(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)

-- 
DROP FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone);

CREATE OR REPLACE FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(
	"accountId" integer[] DEFAULT NULL::integer[],
	"providerId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer,
	"roleId" integer[] DEFAULT NULL::integer[],
	"admissionNo" text DEFAULT NULL::text,
	"patientId" integer DEFAULT NULL::integer,
	"uMRNo" character varying DEFAULT NULL::text,
	"patientMobile" character varying DEFAULT NULL::text,
	"receiptId" integer DEFAULT NULL::integer,
	"createdBy" integer DEFAULT NULL::integer,
	"payTypeId" integer DEFAULT NULL::integer,
	"fromDate" timestamp without time zone DEFAULT (now())::timestamp without time zone,
	"toDate" timestamp without time zone DEFAULT (now())::timestamp without time zone)
    RETURNS TABLE("ReceiptCreatedBy" text, "RoleName" character varying,
				  "ReceiptDate" timestamp without time zone, "ReceiptId" text, "AdmissionNo" text,
				  "AdmissionDate" date, "AdmissionTime" text, "PatientName" text,
				  "UMRNo" character varying, "PatientMobile" character varying,
				  "ProviderName" character varying, "PayTypeName" character varying,"PaymentDetails" character varying,
				  "PaidAmount" numeric, "RefundAmount" numeric, "BalanceAmount" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
BEGIN

return query 

select coalesce(A."FullName",'GrandTotal') "ReceiptCreatedBy",A."RoleName",A."CreatedDate"
"ReceiptDate",coalesce(A."ReceiptId"::text,'Total')"ReceiptId"
,A."AdmissionNo",A."AdmissionDate"::date as "AdmissionDate",to_char(A."AdmissionDate" , 'HH12:MI PM' ) "AdmissionTime",
A."PatientName" "PatientName" ,A."UMRNo",A."Mobile" , A."ProviderName" "ProviderName",
A."PayTypeName",A."PaymentDetails",sum(A."PaidAmount") "PaidAmount",sum(A."RefundAmount") "RefundAmount" 

,sum(A."PaidAmount")-sum(A."RefundAmount") "BalanceAmount"
from (

select R."ReceiptId",A."FullName",Rl."RoleName",R."CreatedDate" "CreatedDate",
coalesce(case when "ReceiptTypeId"=1 then R."Cost" end,0) as "PaidAmount" ,
coalesce(case when "ReceiptTypeId"=2 then R."Cost" end,0) as "RefundAmount" ,Ad."AdmissionNo",ad."AdmissionDate",
	ad."AdmissionTime",Ad."LocationId",
pa."FullName" "PatientName",pa."UMRNo",pa."Mobile",pr."FullName" as "ProviderName",
pt."PayTypeName",R."PaymentDetails"
	from "Receipt" R
join "Admission" ad on Ad."AdmissionId"=R."AdmissionId" and ad."Active" is not false
join "Provider" pr on pr."ProviderId"= ad."ProviderId"
join "Patient" pa on Ad."PatientId"=pa."PatientId"
join "Account" A on A."AccountId"=R."CreatedBy"
join "Role" Rl on Rl."RoleId"=A."RoleId"
join "PayType" pt on pt."PayTypeId"=R."PayTypeId"
	where R."Active" is not false and
case when "accountId" is null then 1=1 else  R."CreatedBy"  = any("accountId") end 
and	case when "providerId" is null then 1=1 else  pr."ProviderId"  = "providerId" end 
and case when "locationId" is null then 1=1 else Ad."LocationId" = "locationId" end
	and	case when "roleId" is null then 1=1 else  A."RoleId"  = any("roleId") end  
	and	case when "admissionNo" is null then 1=1 else  Ad."AdmissionNo"  ilike'%'|| "admissionNo"||'%' end
	and	case when "patientId" is null then 1=1 else  pa."PatientId"  = "patientId" end  
	and	case when "uMRNo" is null then 1=1 else  pa."UMRNo"  ilike'%'|| "uMRNo"||'%' end
	and	case when "patientMobile" is null then 1=1 else  pa."Mobile"  ilike'%'|| "patientMobile"||'%' end
		and	case when "receiptId" is null then 1=1 else  R."ReceiptId"  = "receiptId" end  
and 	case when "createdBy" is null then 1=1 else  R."CreatedBy"  = "createdBy" end  
and
case when "payTypeId" is null then 1=1  else R."PayTypeId" ="payTypeId" end  and
case when "fromDate" is null then 1=1 else "fromDate" <=R."CreatedDate" and R."CreatedDate" <="toDate" end 		
) A
GROUP BY GROUPING SETS((A."ReceiptId",A."RoleName",A."FullName",A."CreatedDate",A."PayTypeName",A."PaymentDetails",A."AdmissionNo",
						A."AdmissionDate",A."AdmissionTime",A."PatientName",A."UMRNo",A."Mobile",A."ProviderName" ), (A."FullName",
																									 A."CreatedDate"), ())     
order by A."FullName" 
 ;

END
$BODY$;

--ALTER FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
  --  OWNER TO postgres;

$BODY$;

--ALTER FUNCTION public."udf_uiReport_Receipts_Admissions_Location"(integer[], integer, integer, integer[], text, integer, character varying, character varying, integer, integer, integer, timestamp without time zone, timestamp without time zone)
  --  OWNER TO postgres;
